Built-in Modules > Sendle Class
Sendle Class
Important: This plugin relies on third-party resources. The resources might change or be discontinued without notice. Ensure you test the plugin extensively before production use, implement error handling with a try-catch block, have a backup shipping rate ready, and reach out to our support with detailed descriptions of any unexpected issues.
Overview
The Sendle
class is crafted to provide real-time shipping rates from Sendle courier.
Class Definition
class Sendle {
constructor(sendle_id, sendle_api_key, origin, destination) {
// Class constructor
}
async getRates(parcels) {
// Method to fetch rates
}
}
Arguments
sendle_id
(String): The Sendle ID associated with your Sendle account.sendle_api_key
(String): The API key for your Sendle account.origin
(Object): The "origin" information from the DATA JSON object used in thecalculateShippingRates
function.destination
(Object): The "destination" information from the same DATA JSON object.
Usage Example
import { Sendle } from "./modules.js"
const sendle = new Sendle(env.sendle_id, env.sendle_api_key, DATA.origin, DATA.destination);
const parcels = [
{ weight: 5, weightUnit: 'KG', length: 10, height: 20, width: 30, metricUnit: "CM" },
// ... additional parcels
];
const rates = await sendle.getRates(parcels);
This example showcases how to use the Sendle
class by providing the required account credentials, origin, and destination details. The getRates
method calculates the shipping rates for an array of parcels, each defined by weight, dimensions, and measurement units.
Notes
- For parcels using the metric system, the values for
metricUnit
andweightUnit
should be 'CM' and 'KG', respectively. - For parcels using the imperial system, the values should be 'IN' and 'LB', respectively.
The returned rates
are formatted in the same way as Shopify's shipping rates, allowing for easy integration and consistency. For better security, your Sendle credentials should be saved in the env
object as encrypted secret variables in the JsRates Settings page.
Sample AI Prompt
I'd like a shipping rate calculator that uses the Sendle rates.
The dimensiobns and weight of the parcels are contained in the items metafield namespace object called "carton". Get the first 15 values in the namespace. The dimensions are given in centimeters, and the weight in kilograms.
Sample AI Output
import { enrichItemDetails } from './modules.js';
import { Sendle } from './modules.js';
export async function calculateShippingRates(DATA, env) {
try {
// Enrich item details with 'carton' metafield
DATA = await enrichItemDetails(DATA, [{ namespace: "carton", size: 15 }]);
// Prepare parcels array for Sendle
const parcels = DATA.items.map(item => {
const cartonMetafield = item.metafields && item.metafields.carton;
return {
length: cartonMetafield ? parseFloat(cartonMetafield.length) : 0,
width: cartonMetafield ? parseFloat(cartonMetafield.width) : 0,
height: cartonMetafield ? parseFloat(cartonMetafield.height) : 0,
weight: cartonMetafield ? parseFloat(cartonMetafield.weight) : 0,
weightUnit: 'KG',
metricUnit: 'CM',
quantity: item.quantity
};
}).filter(parcel => parcel.length > 0 && parcel.width > 0 && parcel.height > 0 && parcel.weight > 0);
// Initialize Sendle with environment variables
const sendle = new Sendle(env.sendle_id, env.sendle_api_key, DATA.origin, DATA.destination);
// Get rates from Sendle
const rates = await sendle.getRates(parcels);
return { rates };
} catch (error) {
// Return a rate with price equals to null in case of errors
return { rates: [{ service_name: "", service_code: "", total_price: null, description: "", currency: "", min_delivery_date: "", max_delivery_date: "" }] };
}
}